define(['main'/*, 'tagsinput'*/, 'metisMenu', 'ajaxUpload'], function(){
    $(function(){});
   
    // MetsiMenu
    $('#side-menu li:not(.nav-header)').removeClass('active');
    var nav = $('#side-menu .nav-sys');
    nav.addClass('active');
    nav.find('li').eq(4).addClass('active');
    $('#side-menu').metisMenu();

    /*$(".tagsinput").tagsInput(
        {
            'onAddTag':function(tag)
                {//增加标签的回调函数
                   
                },
            'onRemoveTag':function(tag)
                { //删除标签的回调函数
                  
                },
        }
    );*/

         $("#img").on("change",function(){
            $("#save_div").hide();
            $("#load_div").show();
             $.ajaxFileUpload
                (
                    {
                    url:'/save_img',//用于文件上传的服务器端请求地址
                    secureuri:false,//一般设置为false
                    fileElementId:'img',//文件上传空间的id属性  <input type="file" id="file" name="file" />
                    dataType: 'json',//返回值类型 一般设置为json
                    success: function (data, status)  //服务器成功响应处理函数
                    {
                        if(data.ret == 0){
                            $("#tag_img").val(data.data.img);
                            $("#thumb_middle").val(data.data.img_320);
                            $("#thumb_small").val(data.data.img_200);
                            $(".thumb_small").attr("src",data.data.img_200);
                        }
                    },
                    complete:function(xhr)
                    {
                        $("#save_div").show();
                        $("#load_div").hide();
                    },
                    error: function (data, status, e)//服务器响应失败处理函数
                    {
                        alert("图片上传出错");
                    }
                }
            );
        }
        );

    $('#confirmModal').on('click', '.okBtn', function(event) {

        var tag_id = $('.tagName').attr('tid');
        var params = {
            tag_id:tag_id
        }
        $.get(delTagUrl, params, function(data, textStatus, xhr) {
            var $tips = $('#tipsModal');
            $tips.modal('show');
            if(data.ret == 0){
                $tips.find('.modal-body').html('删除成功');
                $('#tipsModal').on('click', '.close', function(event) {
                    window.location.href = tagListUrl;
                });
            }else{
                $tips.find('.modal-body').html('删除失败');
            }
        });
    }).on('click', '.cancelBtn', function(event) {
        event.preventDefault();
    });


    $('.tagInfoBox').on('click', '.saveTagBtn', function(event) {
        event.preventDefault();
        var eq_tag = $(".tagsinput").val(),
            tag_name = $('.tagName input').val(),
            attr = $('.attr_list option:selected'),
            attr_id = attr.val(),
            attr_name = attr.html(),
            weight = $('.attr14 .level').val();
            tag_id = $('.tagName').attr('tid'),
            pid = $('.pid').val(),
            tag_index = $('#tag_index').val(),
            img = $('#tag_img').val(),
            thumb_middle = $('#thumb_middle').val(),
            thumb_small = $('#thumb_small').val(),
            point = $('#point').val(),
            is_show = $('#is_show').val(),
            size_type = $('#size_type').val(),
            is_cat = $('#is_cat').val(),
            params = {};

        if($.trim(tag_name) == ''){
            alert('请输入TAG名称');
            $('.tagName').click();
            return false;
        }
        if(is_show=="1" && ""==img){
            alert('分类显示必须上传图片');
            return false;
        }

        params = {
            eq_tag:eq_tag,
            weight:weight,
            tag_name:tag_name,
            attr_name:attr_name,
            attr_id:attr_id,
            tag_id:tag_id,
            pid:pid,
            isNew:0,
            img:img,
            thumb_middle:thumb_middle,
            thumb_small:thumb_small,
            point:point,
            is_show:is_show,
            tag_index:tag_index,
            size_type:size_type,
            is_cat:is_cat
        }
        if(!tag_id){
            params.isNew = 1;
        }

            //{"eq_tag": [{"tag_name": "apps"}, {"tag_name": "asdasdsssssssdfsdsdfsdfs"}], 
            //"tag": {"weight": 0, "pid": 0, "attr_name": "Category", "tag_name": "Apparel", "attr_id": 14, "tag_index": 628, "id": 628}}
        $.post('/admin/sys2014/tag_info_edit', params, function(data, textStatus, xhr) {console.log(params);
            if(data.ret == 0){
                if(params.isNew){
                    alert('添加成功');
                    window.location.href = '/admin/sys2014/tag_list/'+attr_id;
                }else{
                    alert('编辑成功');
                    window.location.href = window.location.href;
                }
            }
        });
    

    }).on('change', '.attr_list', function(event) {
        if($(this).val() != 14){
            $('.tagInfoBox .attr14').hide();
        }else{
            $('.tagInfoBox .attr14').show();
        }

    }).on('change', '.level', function(event) {
        var level = $('.level').val();
        if(level != 10){
            level -= 10;
            qyr_weight_list(level);
        }else{
            $('.pid').html('');
        }
    /*}).on('click', '.tagName', function(event) {
        event.preventDefault();
        var $tagName = $('.tagName'),
            $input = $tagName.find('input'),
            $text = $tagName.find('span');

        $input.show();
        $input.focus();
        $text.hide();

    }).on('keydown', '.tagName input', function(event) {
        
        if(event.keyCode == 13){
            event.preventDefault();
            var $tagName = $('.tagName'),
            $input = $tagName.find('input'),
            $text = $tagName.find('span');
            $text.attr('v', $input.val())
            $text.html($input.val());
            $input.hide();
            $text.show();
        }

    }).on('blur', '.tagName input', function(event) {
        event.preventDefault();
        var $tagName = $('.tagName'),
            $input = $tagName.find('input'),
            $text = $tagName.find('span');

        $text.show();
        $input.val($text.attr('v'));
        $input.hide();
*/
    });

    function qyr_weight_list(level){
        $.get('/admin/sys2014/qyr_weight_list', {level:level}, function(data) {
            var tag_list = data.data.tag_list,
                html = '<option value="0">请选择</option>';
            $.each(tag_list, function( key, value) {
                if(tag_id != value.id){
                    html += '<option value="'+value.id+'">'+ value.tag_name +'</option>';
                }
            });
            $('.pid').html(html);
        });
    }
    
});